package com.chandra.pos.payment.service.impl;


import com.chandra.pos.customer.dao.entity.CustomerPaymentEntity;
import com.chandra.pos.customer.dao.entity.CustomerPaymentRepository;
import com.chandra.pos.payment.dao.PaymentRepository;
import com.chandra.pos.payment.dao.entity.PaymentEntity;
import com.chandra.pos.payment.dao.entity.PaymentStatus;
import com.chandra.pos.payment.model.PaymentFilter;
import com.chandra.pos.payment.model.PaymentList;
import com.chandra.pos.payment.model.PaymentModel;
import com.chandra.pos.payment.service.PaymentRole;
import com.chandra.pos.product.dao.entity.ProductRepository;
import org.apache.commons.lang.StringUtils;
import org.joda.time.DateTime;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Component;

import javax.persistence.EntityNotFoundException;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by dnagarajan on 3/28/2015.
 */
@Component
public class PaymentRoleImpl implements PaymentRole {

    @Autowired
    private PaymentRepository paymentRepository;

    @Autowired
    private CustomerPaymentRepository customerPaymentRepository;


    @Autowired
    private ProductRepository productRepository;

    @Override
    public PaymentModel savePayment(PaymentModel paymentModel) {

        PaymentEntity paymentEntity = new PaymentEntity();

        if (paymentModel.getPaymentDate() == null)
            paymentModel.setPaymentDate(DateTime.now());

        BeanUtils.copyProperties(paymentModel, paymentEntity);

        paymentEntity.setPaymentStatus(PaymentStatus.IPS);



        paymentEntity = paymentRepository.save(paymentEntity);
        paymentModel.setPaymentId(paymentEntity.getPaymentId());

        CustomerPaymentEntity customerPaymentEntity = new CustomerPaymentEntity();

        customerPaymentEntity.setCustomerId(paymentModel.getCustomerId());
        customerPaymentEntity.setPaymentId(paymentModel.getPaymentId());

        customerPaymentRepository.save(customerPaymentEntity);


        return paymentModel;

    }


//
//    @Override
//    public PaymentList search(PaymentFilter paymentFilter) {
//
//        PaymentList customerList = new PaymentList();
//
//        List<PaymentModel> customerModels = new ArrayList<>();
//        Page<PaymentEntity> customerEntity1 = null;
//
//        PageRequest request =
//                new PageRequest(paymentFilter.getPage() - 1, paymentFilter.getRows(), paymentFilter.getDBSord(), paymentFilter.getSidx());
//
//        if (!StringUtils.isEmpty(paymentFilter.getQuery()))
//
//            customerEntity1 = paymentRepository.findByPaymentDateBetween(paymentFilter.getFromDate(), paymentFilter.getToDate(), request);
//        else
//            customerEntity1 = paymentRepository.findByPaymentDateBetween(DateTime.now(), DateTime.now().plusDays(1), request);
//
//
//        for (PaymentEntity customerEntity : customerEntity1.getContent()) {
//            PaymentModel customerModel = new PaymentModel();
//
//            BeanUtils.copyProperties(customerEntity, customerModel);
//
//            customerModels.add(customerModel);
//        }
//        customerList.setPage(paymentFilter.getPage());
//        customerList.setRecords(customerEntity1.getTotalElements());
//        customerList.setTotal(customerEntity1.getTotalPages());
//
//        customerList.setPayments(customerModels);
//        return customerList;
//
//    }

    @Override
    public PaymentList search(PaymentFilter orderFilter) {

        PaymentList paymentList = new PaymentList();

        List<PaymentModel> paymentModels = new ArrayList<>();
        Page<PaymentEntity> paymentEntities = null;

        PageRequest request =
                new PageRequest(orderFilter.getPage() - 1, orderFilter.getRows(), orderFilter.getDBSord(), orderFilter.getSidx());

        if (orderFilter.getId() != null) {
            PaymentEntity paymentEntity = paymentRepository.findOne(orderFilter.getId());
            PaymentModel paymentModel = new PaymentModel();

            BeanUtils.copyProperties(paymentEntity, paymentModel);

            paymentModels.add(paymentModel);

            paymentList.setPayments(paymentModels);
            return paymentList;


        } else if (!StringUtils.isEmpty(orderFilter.getQuery()))

            paymentEntities = paymentRepository.findByPaymentDateBetween(orderFilter.getFromDate(), orderFilter.getToDate(), request);
        else if (orderFilter.getFromDate() != null && orderFilter.getToDate() != null && orderFilter.getPaymentMethod() != null)
            paymentEntities = paymentRepository.searchPayments1(orderFilter.getPaymentMethod(), orderFilter.getFromDate(), orderFilter.getToDate(), request);
        else if (orderFilter.getFromDate() != null && orderFilter.getToDate() != null && orderFilter.getPaymentMethod() != null && orderFilter.getPaymentStatus() != null)
            paymentEntities = paymentRepository.searchPayments2(orderFilter.getPaymentMethod(), orderFilter.getPaymentStatus(), orderFilter.getFromDate(), orderFilter.getToDate(), request);
        else
            paymentEntities = paymentRepository.findByPaymentDateBetween(DateTime.now().withTimeAtStartOfDay(), DateTime.now().plusDays(1), request);


        for (PaymentEntity customerEntity : paymentEntities.getContent()) {
            PaymentModel paymentModel = new PaymentModel();

            BeanUtils.copyProperties(customerEntity, paymentModel);

            paymentModels.add(paymentModel);
        }
        paymentList.setPage(orderFilter.getPage());
        paymentList.setRecords(paymentEntities.getTotalElements());
        paymentList.setTotal(paymentEntities.getTotalPages());

        paymentList.setPayments(paymentModels);
        return paymentList;

    }


    @Override
    public PaymentModel getPaymentById(Long paymentId) throws EntityNotFoundException {

        PaymentModel customerModel = new PaymentModel();

        PaymentEntity customerEntity1 = paymentRepository.findOne(paymentId);
        if (customerEntity1 == null)
            throw new EntityNotFoundException("dsds");
        BeanUtils.copyProperties(customerEntity1, customerModel);


        return customerModel;
    }
}
